package yue.shen.android.demo.select

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.luck.picture.lib.entity.LocalMedia
import com.luck.picture.lib.entity.LocalMediaFolder
import yue.shen.android.demo.R


class SelectFolderAdapter(private val data: ArrayList<LocalMediaFolder>) :
    RecyclerView.Adapter<SelectFolderViewHolder>() {

    private var listener: SelectFolderListener? = null

    fun bindFolderData(data: ArrayList<LocalMediaFolder>) {
        this.data.clear()
        this.data.addAll(data)
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SelectFolderViewHolder {
        return SelectFolderViewHolder(
            LayoutInflater.from(parent.context)
                .inflate(R.layout.picture_select_folder_item, parent, false)
        )
    }

    override fun onBindViewHolder(holder: SelectFolderViewHolder, position: Int) {
        val item = data[position]
        holder.bindData(item)
        holder.itemView.setOnClickListener {
            listener?.onItemClick(item.name, item.images as ArrayList<LocalMedia>)
        }
    }

    override fun getItemCount(): Int {
        return data.size
    }

    fun setOnItemClickListener(listener: SelectFolderListener) {
        this.listener = listener
    }

}